/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.stereotype.Service
 *  tk.mybatis.mapper.entity.Example
 */
package com.gys.spliteService.impl;

import cn.hutool.core.util.ObjectUtil;
import com.gys.mapper.GaiaSdPaymentMethodMapper;
import com.gys.mapper.GaiaSdRechargeCardMapper;
import com.gys.mapper.GaiaSdSaleDMapper;
import com.gys.mapper.GaiaSdSaleHMapper;
import com.gys.mapper.GaiaSdStockBatchMapper;
import com.gys.mapper.GaiaSdStockMapper;
import com.gys.mapper.entity.GaiaSdRechargeCard;
import com.gys.mapper.entity.GaiaSdSaleD;
import com.gys.mapper.entity.GaiaSdSaleH;
import com.gys.mapper.entity.GaiaSdStock;
import com.gys.mapper.entity.GaiaSdStockBatch;
import com.gys.spliteService.PayService;
import com.gys.spliteService.data.GetDzqInData;
import com.gys.spliteService.data.GetPayInData;
import com.gys.spliteService.data.GetPayOutData;
import com.gys.spliteService.data.GetPayTypeInData;
import com.gys.spliteService.data.GetPayTypeOutData;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class PayServiceImpl
implements PayService {
    @Autowired
    private GaiaSdPaymentMethodMapper paymentMethodMapper;
    @Autowired
    private GaiaSdSaleHMapper saleHMapper;
    @Autowired
    private GaiaSdSaleDMapper saleDMapper;
    @Autowired
    private GaiaSdStockMapper stockMapper;
    @Autowired
    private GaiaSdStockBatchMapper stockBatchMapper;
    @Autowired
    private GaiaSdRechargeCardMapper rechargeCardMapper;

    @Override
    public List<GetPayTypeOutData> payTypeList(GetPayInData inData) {
        List<GetPayTypeOutData> outData = this.paymentMethodMapper.payTypeList(inData);
        return outData;
    }

    @Override
    public GetPayOutData payInfo(GetPayInData inData) {
        GetPayOutData outData = new GetPayOutData();
        Example example = new Example(GaiaSdSaleH.class);
        example.createCriteria().andEqualTo("clientId", inData.getClientId()).andEqualTo("gsshBillNo", inData.getBillNo());
        GaiaSdSaleH saleH = (GaiaSdSaleH)this.saleHMapper.selectOneByExample(example);
        outData.setBillNo(saleH.getGsshBillNo());
        outData.setYsAmount(saleH.getGsshYsAmt().setScale(2, 4));
        BigDecimal gsshIntegralCashAmt = saleH.getGsshIntegralCashAmt() == null ? BigDecimal.ZERO : saleH.getGsshIntegralCashAmt();
        BigDecimal gsshDyqAmt = saleH.getGsshDyqAmt() == null ? BigDecimal.ZERO : saleH.getGsshDyqAmt();
        BigDecimal gsshDzqdyAmt1 = saleH.getGsshDzqdyAmt1() == null ? BigDecimal.ZERO : saleH.getGsshDzqdyAmt1();
        BigDecimal gsshDzqdyAmt2 = saleH.getGsshDzqdyAmt2() == null ? BigDecimal.ZERO : saleH.getGsshDzqdyAmt2();
        BigDecimal gsshDzqdyAmt3 = saleH.getGsshDzqdyAmt3() == null ? BigDecimal.ZERO : saleH.getGsshDzqdyAmt3();
        BigDecimal gsshDzqdyAmt4 = saleH.getGsshDzqdyAmt4() == null ? BigDecimal.ZERO : saleH.getGsshDzqdyAmt4();
        BigDecimal gsshDzqdyAmt5 = saleH.getGsshDzqdyAmt5() == null ? BigDecimal.ZERO : saleH.getGsshDzqdyAmt5();
        outData.setDkAmount(gsshIntegralCashAmt.add(gsshDyqAmt).add(gsshDzqdyAmt1).add(gsshDzqdyAmt2).add(gsshDzqdyAmt3).add(gsshDzqdyAmt4).add(gsshDzqdyAmt5).setScale(2, 4));
        BigDecimal gsshRmbAmt = saleH.getGsshRmbAmt() == null ? BigDecimal.ZERO : saleH.getGsshRmbAmt();
        BigDecimal gsshRechargeCardAmt = saleH.getGsshRechargeCardAmt() == null ? BigDecimal.ZERO : saleH.getGsshRechargeCardAmt();
        BigDecimal gsshPaymentAmt1 = saleH.getGsshPaymentAmt1() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt1();
        BigDecimal gsshPaymentAmt2 = saleH.getGsshPaymentAmt2() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt2();
        BigDecimal gsshPaymentAmt3 = saleH.getGsshPaymentAmt3() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt3();
        BigDecimal gsshPaymentAmt4 = saleH.getGsshPaymentAmt4() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt4();
        BigDecimal gsshPaymentAmt5 = saleH.getGsshPaymentAmt5() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt5();
        outData.setSsAmount(gsshRmbAmt.add(gsshRechargeCardAmt).add(gsshPaymentAmt1).add(gsshPaymentAmt2).add(gsshPaymentAmt3).add(gsshPaymentAmt4).add(gsshPaymentAmt5).setScale(2, 4));
        outData.setDsAmount(outData.getYsAmount().subtract(outData.getSsAmount()).setScale(2, 4));
        outData.setZlAmount((saleH.getGsshRmbZlAmt() == null ? BigDecimal.ZERO : saleH.getGsshRmbZlAmt()).setScale(2, 4));
        List<GetPayTypeOutData> payTypeList = this.paymentMethodMapper.payTypeList(inData);
        for (GetPayTypeOutData payTypeOutData : payTypeList) {
            payTypeOutData.setPayAmount(BigDecimal.ZERO.setScale(2, 4));
            if ("1".equals(payTypeOutData.getGspmType())) {
                if (payTypeOutData.getGspmId().equals(saleH.getGsshPaymentNo1())) {
                    payTypeOutData.setPayAmount((saleH.getGsshPaymentAmt1() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt1()).setScale(2, 4));
                }
                if (payTypeOutData.getGspmId().equals(saleH.getGsshPaymentNo2())) {
                    payTypeOutData.setPayAmount((saleH.getGsshPaymentAmt2() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt2()).setScale(2, 4));
                }
                if (payTypeOutData.getGspmId().equals(saleH.getGsshPaymentNo3())) {
                    payTypeOutData.setPayAmount((saleH.getGsshPaymentAmt3() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt3()).setScale(2, 4));
                }
                if (payTypeOutData.getGspmId().equals(saleH.getGsshPaymentNo4())) {
                    payTypeOutData.setPayAmount((saleH.getGsshPaymentAmt4() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt4()).setScale(2, 4));
                }
                if (payTypeOutData.getGspmId().equals(saleH.getGsshPaymentNo5())) {
                    payTypeOutData.setPayAmount((saleH.getGsshPaymentAmt5() == null ? BigDecimal.ZERO : saleH.getGsshPaymentAmt5()).setScale(2, 4));
                }
                if (!"1000".equals(payTypeOutData.getGspmId())) continue;
                payTypeOutData.setPayAmount((saleH.getGsshRmbAmt() == null ? BigDecimal.ZERO : saleH.getGsshRmbAmt()).setScale(2, 4));
                continue;
            }
            if ("9001".equals(payTypeOutData.getGspmId())) {
                payTypeOutData.setPayAmount((saleH.getGsshIntegralCashAmt() == null ? BigDecimal.ZERO : saleH.getGsshIntegralCashAmt()).setScale(2, 4));
            }
            if ("9002".equals(payTypeOutData.getGspmId())) {
                payTypeOutData.setPayAmount(gsshDzqdyAmt1.add(gsshDzqdyAmt1).add(gsshDzqdyAmt1).add(gsshDzqdyAmt1).add(gsshDzqdyAmt1).setScale(2, 4));
            }
            if ("9003".equals(payTypeOutData.getGspmId())) {
                payTypeOutData.setPayAmount((saleH.getGsshDyqAmt() == null ? BigDecimal.ZERO : saleH.getGsshDyqAmt()).setScale(2, 4));
            }
            if (!"8000".equals(payTypeOutData.getGspmId())) continue;
            payTypeOutData.setPayAmount((saleH.getGsshRechargeCardAmt() == null ? BigDecimal.ZERO : saleH.getGsshRechargeCardAmt()).setScale(2, 4));
        }
        outData.setPayTypeList(payTypeList);
        return outData;
    }

    @Override
    public void payCommit(GetPayInData inData) {
        Example example = new Example(GaiaSdSaleH.class);
        example.createCriteria().andEqualTo("clientId", inData.getClientId()).andEqualTo("gsshBillNo", inData.getBillNo());
        GaiaSdSaleH saleH = (GaiaSdSaleH)this.saleHMapper.selectOneByExample(example);
        List<GetPayTypeInData> payTypeList = inData.getPayTypeList();
        for (GetPayTypeInData payTypeInData : payTypeList) {
            if (!"1".equals(payTypeInData.getGspmType())) continue;
            if (ObjectUtil.isEmpty(saleH.getGsshPaymentNo1())) {
                saleH.setGsshPaymentNo1(payTypeInData.getGspmId());
                saleH.setGsshPaymentAmt1(payTypeInData.getPayAmount());
            } else if (ObjectUtil.isEmpty(saleH.getGsshPaymentNo2())) {
                saleH.setGsshPaymentNo2(payTypeInData.getGspmId());
                saleH.setGsshPaymentAmt2(payTypeInData.getPayAmount());
            } else if (ObjectUtil.isEmpty(saleH.getGsshPaymentNo3())) {
                saleH.setGsshPaymentNo3(payTypeInData.getGspmId());
                saleH.setGsshPaymentAmt3(payTypeInData.getPayAmount());
            } else if (ObjectUtil.isEmpty(saleH.getGsshPaymentNo4())) {
                saleH.setGsshPaymentNo4(payTypeInData.getGspmId());
                saleH.setGsshPaymentAmt4(payTypeInData.getPayAmount());
            } else {
                saleH.setGsshPaymentNo5(payTypeInData.getGspmId());
                saleH.setGsshPaymentAmt5(payTypeInData.getPayAmount());
            }
            if (!"1000".equals(payTypeInData.getGspmId())) continue;
            saleH.setGsshRmbAmt(payTypeInData.getPayAmount());
        }
        saleH.setGsshDyqType(inData.getDyqReason());
        saleH.setGsshDyqAmt(inData.getDyqAmount());
        saleH.setGsshDyqNo(inData.getDyqNo());
        saleH.setGsshRechargeCardNo(inData.getCzkNo());
        saleH.setGsshRechargeCardAmt(inData.getCzkAmount());
        saleH.setGsshHideFlag("0");
        saleH.setGsshRmbZlAmt(inData.getZlAmount());
        saleH.setGsshZkAmt(inData.getDkAmount());
        this.saleHMapper.updateByPrimaryKeySelective(saleH);
        Example exampleD = new Example(GaiaSdSaleD.class);
        exampleD.createCriteria().andEqualTo("clientId", inData.getClientId()).andEqualTo("gssdBillNo", inData.getBillNo()).andEqualTo("gssdBrId", inData.getStoreCode());
        List<GaiaSdSaleD> saleDList = this.saleDMapper.selectByExample(exampleD);
        for (GaiaSdSaleD saleD : saleDList) {
            BigDecimal rate = saleD.getGssdAmt().divide(saleH.getGsshYsAmt());
            saleD.setGssdZkJfdx(inData.getJfdxAmount().multiply(rate).setScale(2, 4));
            saleD.setGssdZkDyq(inData.getDyqAmount().multiply(rate).setScale(2, 4));
            for (GetDzqInData dzqInData : inData.getDzqList()) {
                if (!ObjectUtil.isNotEmpty(dzqInData.getProCode()) || !saleD.getGssdProId().equals(dzqInData.getProCode())) continue;
                saleD.setGssdZkDzq(saleD.getGssdZkDzq().add(dzqInData.getGspcbCouponAmt().multiply(new BigDecimal(dzqInData.getGspcbCount()))).setScale(2, 4));
            }
            saleD.setGssdZkAmt(saleD.getGssdZkJfdx().add(saleD.getGssdZkDzq()).add(saleD.getGssdZkDyq()));
            this.saleDMapper.updateByPrimaryKeySelective(saleD);
            Example exampleStock = new Example(GaiaSdStock.class);
            exampleStock.createCriteria().andEqualTo("clientId", inData.getClientId()).andEqualTo("gssBrId", inData.getStoreCode()).andEqualTo("gssProId", saleD.getGssdProId());
            GaiaSdStock stockFrom = (GaiaSdStock)this.stockMapper.selectOneByExample(exampleStock);
            if (ObjectUtil.isNotEmpty(stockFrom)) {
                stockFrom.setGssQty(new BigDecimal(stockFrom.getGssQty()).subtract(new BigDecimal(saleD.getGssdQty())).toString());
                this.stockMapper.updateByPrimaryKeySelective(stockFrom);
            }
            Example exampleBatch = new Example(GaiaSdStockBatch.class);
            exampleBatch.createCriteria().andEqualTo("clientId", inData.getClientId()).andEqualTo("gssbBrId", inData.getStoreCode()).andEqualTo("gssbProId", saleD.getGssdProId()).andEqualTo("gssbBatchNo", saleD.getGssdBatchNo()).andEqualTo("gssbBatch", saleD.getGssdBatch());
            GaiaSdStockBatch stockBatchFrom = (GaiaSdStockBatch)this.stockBatchMapper.selectOneByExample(exampleBatch);
            if (!ObjectUtil.isNotEmpty(stockBatchFrom)) continue;
            stockBatchFrom.setGssbQty(new BigDecimal(stockBatchFrom.getGssbQty()).subtract(new BigDecimal(saleD.getGssdQty())).toString());
            this.stockBatchMapper.updateByPrimaryKeySelective(stockBatchFrom);
        }
        if (ObjectUtil.isNotEmpty(inData.getCzkNo())) {
            Example exampleRecharge = new Example(GaiaSdRechargeCard.class);
            exampleRecharge.createCriteria().andEqualTo("clientId", inData.getClientId()).andEqualTo("gsrcId", inData.getCzkNo()).andEqualTo("gsrcStatus", "1");
            GaiaSdRechargeCard rechargeCard2 = (GaiaSdRechargeCard)this.rechargeCardMapper.selectOneByExample(exampleRecharge);
            rechargeCard2.setGsrcAmt(rechargeCard2.getGsrcAmt().subtract(inData.getCzkAmount()));
            this.rechargeCardMapper.updateByPrimaryKeySelective(rechargeCard2);
        }
    }
}

